package com.orangelife.mobile.point.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.curry.android.util.TimeUtil;
import com.curry.orangelife.mobile.R;
import com.curry.android.adapter.FatherAdapter;
import com.orangelife.mobile.util.DateUtil;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 橙汁明细
 * 2016年5月9日 14:36:03
 */
public class MyPointDetailAdapter<T> extends FatherAdapter<T> {

    private Context mContext;
    private List<Map<String, Object>> list;

    public MyPointDetailAdapter(Context context,
                                List<Map<String, Object>> list) {
        this.mContext = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        DateUtil dateUtil = new DateUtil();
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_lv_my_score, null);
            holder.tvDate = (TextView) convertView.findViewById(R.id.tvDate);
            holder.tvItemDate = (TextView) convertView.findViewById(R.id.tvItemDate);
            holder.tvScore = (TextView) convertView.findViewById(R.id.tvScore);
            holder.tvScoreType = (TextView) convertView.findViewById(R.id.tvScoreType);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String time =list.get(position).get("tsCreate").toString();
        String points = list.get(position).get("points").toString();//积分
        String pRuleWname = list.get(position).get("pRuleWname").toString();//类型
        String pStat = list.get(position).get("pStat").toString();//积分操作
        holder.tvDate.setText(parseDate2(time));
        if (position != 0) {
            String timeBefore = String.valueOf(list.get(position - 1).get("tsCreate"));
            if (parseDate2(timeBefore).equals(parseDate2(time))) {
                holder.tvDate.setVisibility(View.GONE);
            } else {
                holder.tvDate.setVisibility(View.VISIBLE);
            }
        } else {
            holder.tvDate.setVisibility(View.VISIBLE);
        }

        if (pStat.equals("减")) {
            holder.tvScore.setTextColor(mContext.getResources().getColor(R.color.text_black));
        } else if (pStat.equals("加")) {
            holder.tvScore.setTextColor(mContext.getResources().getColor(R.color.lightOrange));
        } else if (pStat.equals("冻结")) {
            holder.tvScore.setTextColor(mContext.getResources().getColor(R.color.text_black));
        }


        holder.tvScoreType.setText(pRuleWname);
        holder.tvItemDate.setText(DateUtil.getInstance().tranStringForDate(time));
        holder.tvScore.setText(points);
        return convertView;
    }

    class ViewHolder {
        /* 时间 */
        private TextView tvItemDate;
        /* 内容 */
        private TextView tvScore;
        /* 分享类型 */
        private TextView tvScoreType;

        private TextView tvDate;
    }

    @SuppressWarnings("unchecked")
    @Override
    public BaseAdapter setList(List<T> list) {
        this.list = (List<Map<String, Object>>) list;
        return MyPointDetailAdapter.this;
    }

    /**
     * 处理时间的方法只显示日期
     *
     * @param createTime
     * @return
     */

    public String parseDate2(String createTime) {
        createTime = TimeUtil.timeStampToDate(createTime, "yyyy-MM-dd HH:mm");
        SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (createTime == null || "".equals(createTime)) {
            return "";
        }
        Date date = null;
        try {
            date = format.parse(createTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar current = Calendar.getInstance();

        Calendar today = Calendar.getInstance();    //今天

        today.set(Calendar.YEAR, current.get(Calendar.YEAR));
        today.set(Calendar.MONTH, current.get(Calendar.MONTH));
        today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
        //  Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);

        Calendar yesterday = Calendar.getInstance();    //昨天

        yesterday.set(Calendar.YEAR, current.get(Calendar.YEAR));
        yesterday.set(Calendar.MONTH, current.get(Calendar.MONTH));
        yesterday.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH) - 1);
        yesterday.set(Calendar.HOUR_OF_DAY, 0);
        yesterday.set(Calendar.MINUTE, 0);
        yesterday.set(Calendar.SECOND, 0);

        current.setTime(date);

        if (current.after(today)) {
            return "今天 ";
        } else if (current.before(today) && current.after(yesterday)) {

            return "昨天 ";
        } else {
            int index = createTime.indexOf(" ") + 1;
            return createTime.substring(0, index);
        }
    }
}